using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


public partial class usercontrol_ucPageControl : System.Web.UI.UserControl
{
    #region Declaration
    /// <summary>
    /// Gloabal Object Declaration for Variables and Properties
    /// </summary>
    private int inttotalnoofrecords;
    private int intTotalPages;
    private int intCurrPageNumber;
    private int intstartRowIndex;
    private bool blnExpExcel;
  
    #region Events Declaration
    public delegate void OnButtonClick();
    public event OnButtonClick btnHandler;      
    public event EventHandler ExportToExcel;  
    public event EventHandler dropdownchange;
    #endregion

    #region Properties
    public int TotalRecords
    {
        get
        {
            if (ViewState["TotalRecords"] != null)
                return Convert.ToInt32(ViewState["TotalRecords"].ToString());
            else
                return 0;
        }


        set { ViewState["TotalRecords"] = value; txtHidCurrPage.Text = "1"; BindNavLinks(); }

    }
    public int RecordsPerPage
    {
        get
        {
            if (ViewState["RecordsPerPage"] != null)
            {

                return Convert.ToInt32(ViewState["RecordsPerPage"].ToString());
            }
            else
                return 10;
        }
        set { ViewState["RecordsPerPage"] = value; ddRecords.SelectedIndex = ddRecords.Items.IndexOf(ddRecords.Items.FindByValue(Convert.ToString(this.RecordsPerPage))); }
    }
    public int CurrPageNumber
    {
        get { return intCurrPageNumber; }
        set { intCurrPageNumber = value; }
    }
  
  
    public bool ExpExcelIsEnabled
    {
        get
        {
            return blnExpExcel;
        }
        set
        {
            blnExpExcel = value;
        }
    }
    public bool GotoPageIsEnable
    {

        set
        {
            if (value) txtHidCurrPage.Visible = true; else txtHidCurrPage.Visible = false;
        }
    }
  
    public bool ExportExcelIsVisible
    {

        set { if (value) tdExcel.Visible = true; else tdExcel.Visible = false; }
    }
    public int RowIndex
    {
        get { return intstartRowIndex; }
        set { intstartRowIndex = value; }
    }

    #endregion
    #endregion

    #region ControlMethods
    /// <summary>
    /// Name of Method                    : Page_Load
    /// Description                       : This method is used to Load Page
    /// </summary>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindNavLinks();
            if (Convert.ToString(txtHidCurrPage.Text.Length.ToString()) == "1")
            {
                TDlblof.Attributes.Add("style", "width: 7px;");
            }
            if (Convert.ToString(txtHidCurrPage.Text.Length.ToString()) == "4")
            {
                TDlblof.Attributes.Add("style", "width: 15px;");
            }
        }
        //btnExportExcel.OnClientClick = "needToConfirm = false;";

    }
    /// <summary>
    /// This method is called when ever user clicks on the First,Last ,
    /// Previous,Next buttons on this user control.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void NavigationLink_Click(Object sender, CommandEventArgs e)
    {

        switch (e.CommandName)
        {
            case "First":
                intCurrPageNumber = 1;

                txtHidCurrPage.Text = intCurrPageNumber.ToString();
                break;
            case "Last":
                intCurrPageNumber = Convert.ToInt32(lblHidTotalPage.Text);


                txtHidCurrPage.Text = intCurrPageNumber.ToString();
                break;
            case "Next":



                intCurrPageNumber = Convert.ToInt16(txtHidCurrPage.Text.ToString());
                if (intCurrPageNumber <= (int.Parse(lblHidTotalPage.Text) - 1))
                {
                    intCurrPageNumber++;


                    txtHidCurrPage.Text = intCurrPageNumber.ToString();
                }
                break;
            case "Prev":



                intCurrPageNumber = Convert.ToInt16(txtHidCurrPage.Text.ToString());
                if (intCurrPageNumber > 1)
                {
                    intCurrPageNumber--;


                    txtHidCurrPage.Text = intCurrPageNumber.ToString();
                }
                break;
        }
        if (btnHandler != null)
        {

            this.RowIndex = (Convert.ToInt32(txtHidCurrPage.Text) - 1) * this.RecordsPerPage + 1;
            if (Convert.ToInt16((txtHidCurrPage.Text).ToString()) != 0)
                btnHandler();
        }

        BindNavLinks();
    }
    /// <summary>
    /// Name of Method                    : OnExportClick
    /// Description                       : This method is used fire exportexcel event
    /// </summary>
    protected void OnExportClick(EventArgs e)
    {
        if (ExportToExcel != null)
        {
            ExportToExcel(this, e);
        }
    } 
    /// <summary>
    /// This method will be called if the user has given any input in the pager control's
    /// textbox and after pressing the tab
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void txtHidCurrPage_TextChanged(object sender, EventArgs e)
    {

        if (txtHidCurrPage.Text != "")
        {
            if ((Convert.ToInt32(txtHidCurrPage.Text) <= Convert.ToInt32(lblHidTotalPage.Text)) && (Convert.ToInt32(txtHidCurrPage.Text) > 0))
            {
                if (btnHandler != null)
                {
                    intCurrPageNumber = Convert.ToInt16(txtHidCurrPage.Text.ToString());

                    this.RowIndex = (Convert.ToInt32(txtHidCurrPage.Text) - 1) * this.RecordsPerPage + 1;
                    btnHandler();
                }

                BindNavLinks();
            }

            if (Convert.ToInt32(txtHidCurrPage.Text.Trim()) > Convert.ToInt32(lblHidTotalPage.Text))
            {
                txtHidCurrPage.Text = "1";
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "strPageValida", "alert('" + Resources.Global.jsErrCurrentPageExceedsTotalPages + "')", true);
                btnHandler();
                return;
            }
            else if (Convert.ToInt32(txtHidCurrPage.Text.Trim()) < 1)
            {
                txtHidCurrPage.Text = "1";

                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "strPageValidation", "alert('" + Resources.Global.jsErrCurrentPageNotLess1 + "')", true);
                btnHandler();
                return;
            }
        }
        if (txtHidCurrPage.Text != "")
        {

        }

    }
    /// <summary>
    /// This method will be called if the user has change pager control's
    /// textbox and after pressing the tab
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddRecords_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (dropdownchange != null)
        {
            this.RecordsPerPage = Convert.ToInt32(ddRecords.SelectedItem.Text);
            txtHidCurrPage.Text = "";
            intCurrPageNumber = 1;
            BindNavLinks();
            dropdownchange(this, e);
        }

    }

    #endregion

    #region UserDefinedFunction
    /// <summary>
    /// Name of Method                    : BindNavLinks
    /// Description                       : This method is used fire next and prev btns
    /// </summary>
    private void BindNavLinks()
    {
        if (RecordsPerPage == 0)
        {
            this.lblPage.Visible = false;
            this.lblOf.Visible = false;

            txtHidCurrPage.Visible = false;

            this.lblHidTotalPage.Visible = false;
            this.imgbtnFirst.Enabled = false;
            this.imgbtnLast.Enabled = false;
            this.imgbtnNext.Enabled = false;
            this.imgbtnPrev.Enabled = false;
        }
        else
            if (RecordsPerPage == TotalRecords)
            {
                this.lblPage.Visible = true;
                this.lblOf.Visible = true;


                txtHidCurrPage.Visible = true;

                this.lblHidTotalPage.Visible = true;
                this.imgbtnFirst.Enabled = false;
                this.imgbtnLast.Enabled = false;
                this.imgbtnNext.Enabled = false;
                this.imgbtnPrev.Enabled = false;
            }
            else
            {
                this.lblPage.Visible = true;
                this.lblOf.Visible = true;

                txtHidCurrPage.Visible = true;

                this.lblHidTotalPage.Visible = true;

                this.imgbtnFirst.Enabled = true;
                this.imgbtnLast.Enabled = true;
                this.imgbtnNext.Enabled = true;
                this.imgbtnPrev.Enabled = true;
            }
        if (!Page.IsPostBack)
        {
            inttotalnoofrecords = (int)System.Math.Ceiling((double)TotalRecords);
            Totalnoofrecords.Text = inttotalnoofrecords.ToString();
            intTotalPages = (int)System.Math.Ceiling((double)TotalRecords / this.RecordsPerPage);
            lblHidTotalPage.Text = intTotalPages.ToString();

            intCurrPageNumber = 1;


            txtHidCurrPage.Text = intCurrPageNumber.ToString();
        }
        else
        {
            inttotalnoofrecords = (int)System.Math.Ceiling((double)TotalRecords);
            Totalnoofrecords.Text = inttotalnoofrecords.ToString();
            intTotalPages = (int)System.Math.Ceiling((double)TotalRecords / this.RecordsPerPage);
            lblHidTotalPage.Text = intTotalPages.ToString();

            if (this.CurrPageNumber > 0)
            {


                txtHidCurrPage.Text = intCurrPageNumber.ToString();
            }

            intCurrPageNumber = int.Parse(txtHidCurrPage.Text);

            txtHidCurrPage.Text = intCurrPageNumber.ToString();
        }
        if ((intCurrPageNumber == 1))
        {
            this.imgbtnFirst.Enabled = false;
            this.imgbtnPrev.Enabled = false;

            if ((intTotalPages > 1))
            {
                this.imgbtnNext.Enabled = true;
                this.imgbtnLast.Enabled = true;
            }
            else
            {
                this.imgbtnLast.Enabled = false;
                this.imgbtnNext.Enabled = false;

            }
        }
        else
        {
            this.imgbtnPrev.Enabled = true;
            if ((intCurrPageNumber == intTotalPages))
            {
                this.imgbtnLast.Enabled = false;
                this.imgbtnNext.Enabled = false;
            }
            else
            {
                this.imgbtnNext.Enabled = true;
            }
        }
        if (intCurrPageNumber > 0)
        {
            if (TotalRecords > this.RecordsPerPage)


                this.CurrPageNumber = (((Convert.ToInt32(txtHidCurrPage.Text) * 10) + 1) / this.RecordsPerPage) + 1;
            else
                this.CurrPageNumber = 1;
        }
        this.CurrPageNumber = 0;
    }

    #endregion   
}